import menuConfig from '../common/config/menu'

const loadingNotify = p => {
  return p.then(r => {
    return r
  })
}
const docsChildrenRoute = []
const routeMap = {
  path: 'docs',
  redirect: './docs/introduction',
  component: () =>
    loadingNotify(
      import('../components/docs/index.vue')),
  children: docsChildrenRoute
}
Object.keys(menuConfig).forEach(name => {
  getSubList(menuConfig[name]).forEach(key => {
    if (key !== 'introduction') {
      docsChildrenRoute.push({
        path: key,
        component: () =>
          loadingNotify(
            import(`../../src/components/packages/${key}/doc.md`))
      })
    }
  })
})
docsChildrenRoute.push({
  path: 'introduction',
  component: () =>
    loadingNotify(
      import('../common/doc-data/introduction.md'))
})

export default [routeMap]

function getSubList(group) {
  let subList = []
  Object.keys(group.subList).forEach(key => {
    const item = group.subList[key]
    if (typeof item === 'string') {
      subList.push(key)
    } else {
      subList = subList.concat(getSubList(item))
    }
  })
  return subList
}
